using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParallaxBackground : MonoBehaviour
{
    [Header("Reference")]
    [SerializeField] GameObject mainCamera;

    [Header("Attribute")]
    [SerializeField] float effect;

    float xPosition;
    float length;

    void Start()
    {
        xPosition = transform.position.x;
        length = GetComponent<SpriteRenderer>().bounds.size.x;
    }

    void Update()
    {        
        float distance = mainCamera.transform.position.x * effect;
        transform.position = new Vector3(xPosition + distance, transform.position.y);

        float moved = mainCamera.transform.position.x * (1 - effect);
        if (moved > xPosition + length)
            xPosition += length;
        else if (moved < xPosition - length)
            xPosition -= length;
    }
}
